<?php

#    Document   : login
#    Created on : May 4, 2011, 2:45:48 PM
#    Author     : ClarkT AKA FalleN
#    Company    : Amodity
#    Web        : http://www.amodity.com
#    Copyright  : 2011
/**

 * @property CI_Loader $load

 * @property CI_Form_validation $form_validation

 * @property CI_Input $input

 * @property CI_Email $email

 * @property CI_DB_active_record $db

 * @property CI_DB_forge $dbforge

 *  @property CI_Session $session

 * @property CI_FTP $ftp

 */

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
    }

    function index() {
        $this->template->set('title', lang('g_login'));
        $this->template->set('bodyid', 'login');
        $this->template->load('site_template', 'login/login_form');
    }

    function validate_credentials() {
        if ($this->auth->login($this->input->post('username'), $this->input->post('password'))) {
            redirect('upload');
        } else {
            $error = $this->auth->errors();
            $this->message->set('error', $error);
            $this->index();
        }
    }

    function signup() {
        $this->template->set('title', lang('g_register'));
        $this->template->set('bodyid', 'login');
        $this->template->load('site_template', 'signup/signup_form');
    }

    function create_member() {
        $this->load->library('form_validation');
        // filed name, error message, validation rules

        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if ($this->form_validation->run() == FALSE) {
            $this->signup();
        } elseif ($this->auth->username_check($this->input->post('username') == true)) {
            $this->message->set('error', $this->lang->line('err_username_taken'));
            $this->template->set('title', lang('g_signup'));
            $this->template->set('bodyid', 'login');
            $this->template->load('site_template', 'signup/signup_form');
        } else {
            $this->load->model('login_model');
            if ($this->login_model->create_member()) {
                $this->template->set('title', lang('g_signup'));
                $this->template->set('bodyid', 'login');
                $this->template->load('site_template', 'signup/signup_successfull', $data);
            } else {
                $this->load->view('signup/signup_form');
            }
        }
    }

    function _is_logged_in() {
        if (!$this->auth->logged_in()) {
            redirect('upload');
        }
    }

}

?>